<?
/*--|	LIKE
  Software: Socially
   Version: 1.0
   Contact: http://socially.googlecode.com
      Info: http://socially.googlecode.com
   Support: http://socially.googlecode.com
--------------------------------------------------------------------------
    Author: goker.cebeci | http://gokercebeci.com
--------------------------------------------------------------------------
   License: Distributed under the GNU General Public License (GPL)
            http://www.gnu.org/licenses/gpl.html
 This program is distributed in the hope that it will be useful - WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 FITNESS FOR A PARTICULAR PURPOSE.
-------------------------------------------------------------------------*/
// status platonic,match
class iLike {
	/**
	 * $memberOne < $memberTwo ? one : two 
	 *
	 * @param int $memberOne
	 * @param int $memberTwo
	 * @return array
	 */
	private static function _order($memberOne, $memberTwo){
		return ($memberOne < $memberTwo)?array($memberOne,$memberTwo,'one'):array($memberTwo,$memberOne,'two');
	}
	private static function _add($memberOne, $memberTwo, $status = 'platonic'){
		if($memberOne == $memberTwo) return true;
		list($memberOne, $memberTwo, $sender) = self::_order($memberOne, $memberTwo);
		db::query(sprintf("INSERT INTO ilike (memberOne,memberTwo,sender,status,date) 
			VALUES ('%d','%d','%s','%s',NOW())
			ON DUPLICATE KEY UPDATE sender = '%s', status = '%s', `date` = NOW();"
			,$memberOne,$memberTwo,$sender,$status,$sender,$status));
		return true;
	}
	private static function _get($a){
		if($a['list']){
		$r = db::query(sprintf("SELECT IF(memberOne='%s',memberTwo,memberOne) as ID FROM ilike WHERE 1=1",$a['UID'])
			.($a['ID']?sprintf(" AND ID = %d",$a['ID']):'')
			.($a['sender']?sprintf(" AND ((memberOne = %d AND sender = 'one') OR (memberTwo = %d AND sender = 'two'))",$a['UID'],$a['UID']):'')
			.($a['recipient']?sprintf(" AND ((memberOne = %d AND sender = 'two') OR (memberTwo = %d AND sender = 'one'))",$a['UID'],$a['UID']):'')
			.($a['match']?sprintf(" AND (memberOne = %d OR memberTwo = %d)",$a['UID'],$a['UID']):'')
			.($a['status']?sprintf(" AND status = '%s'",$a['status']):'')
			.($a['limit']?sprintf(" LIMIT %d",$a['limit']):'')
			.';',true,true);
			if($r) foreach ($r as $k=>$i){
				$r[$k] = profile::get(array('ID'=>$i->ID));
			}
		} elseif($a['online']){
			$r =  db::query(sprintf("SELECT IF(memberOne='%s',memberTwo,memberOne) as ID FROM ilike 
				WHERE (memberOne = %d OR memberTwo = %d)  
				AND memberOne IN (SELECT ID FROM accountOnline WHERE status <> 'offline') 
				AND memberTwo IN (SELECT ID FROM accountOnline WHERE status <> 'offline') ",$a['UID'],$a['UID'],$a['UID'])
			.($a['status']?sprintf(" AND status = '%s'",$a['status']):'')
			.' ORDER BY ID '
			.($a['limit']?sprintf(" LIMIT %d",$a['limit']):'')
			.';',true,true);
			if($r) foreach ($r as $k=>$i){
				$r[$k] = profile::get(array('ID'=>$i->ID));
			}
		} elseif($a['count']){
		$r = db::query('SELECT COUNT(memberOne) as total FROM ilike WHERE 1=1'
			.($a['sender']?sprintf(" AND ((memberOne = %d AND sender = 'one') OR (memberTwo = %d AND sender = 'two'))",$a['UID'],$a['UID']):'')
			.($a['recipient']?sprintf(" AND ((memberOne = %d AND sender = 'two') OR (memberTwo = %d AND sender = 'one'))",$a['UID'],$a['UID']):'')
			.($a['match']?sprintf(" AND (memberOne = %d OR memberTwo = %d)",$a['UID'],$a['UID']):'')
			.($a['status']?sprintf(" AND status = '%s'",$a['status']):'')
			.';',true);
		$r = $r->total;
		}
		return $r;
	}
/* -----------------------------------------------------------------------
	PUBLIC FUNCTIONS
 ------------------------------------------------------------------------*/
	
/* -----------------------------------------------------------------------
	SPECIAL FUNCTIONS
 ------------------------------------------------------------------------*/
	/**
	 * like platonic function [status = platonic]
	 *
	 * @param big_int $memberOne
	 * @param big_int $memberTwo
	 * @return object
	 */
	public static function like($memberOne, $memberTwo){
		self::_add($memberOne, $memberTwo);		
		return common::result(200);
	}
	/**
	 * like back [update status = match]
	 *
	 * @param big_int $memberOne
	 * @param big_int $memberTwo
	 * @return object
	 */
	public static function likeback($memberOne, $memberTwo){
		if($memberOne == $memberTwo) return common::result(600);
		
		//notification 
		notification::send(array('application'=>'ilike','notification'=>'likeback','sender'=>$memberOne,'recipient'=>$memberTwo));	
		
		$accounID = $memberOne;
		list($memberOne, $memberTwo, $sender) = self::_order($memberOne, $memberTwo);
		$sender = $memberOne == $accounID?'two':'one';
		db::query(sprintf("UPDATE ilike SET status = 'match', `date` = NOW() 
			WHERE memberOne = '%d' AND memberTwo = '%d' AND sender = '%s' AND status = 'platonic' LIMIT 1;",
			$memberOne,$memberTwo,$sender));
		return common::result(200);
	}
	/**
	 * no thanks [call self::unlike]
	 *
	 * @param big_int $memberOne
	 * @param big_int $memberTwo
	 * @return object
	 */
	public static function nothanks($memberOne, $memberTwo){
		return self::unlike($memberOne, $memberTwo);
	}
	/**
	 * unlike [delete record]
	 *
	 * @param big_int $memberOne
	 * @param big_int $memberTwo
	 * @return object
	 */	
	public static function unlike($memberOne, $memberTwo){
		if($memberOne == $memberTwo) return common::result(600);
		list($memberOne, $memberTwo, $sender) = self::_order($memberOne, $memberTwo);
		db::query(sprintf("DELETE FROM ilike 
			WHERE memberOne = '%d' AND memberTwo = '%d' AND status <> 'block' LIMIT 1;",
			$memberOne,$memberTwo));
		return common::result(200);
	}
	/**
	 * people who like me list 
	 *
	 * @param array $a
	 * @return object
	 */
	public static function peopleWhoLikeMe($a){
		$a['recipient'] = true;
		$a['status'] = 'platonic';
		return self::_get($a);
	}
	/**
	 * people I like list 
	 *
	 * @param array $a
	 * @return object
	 */
	public static function peopleiLike($a){
		$a['sender'] = true;
		$a['status'] = 'platonic';
		return self::_get($a);
	}
	/**
	 * match list 
	 *
	 * @param array $a
	 * @return object
	 */
	public static function match($a){
		$a['match'] = true;
		$a['status'] = 'match';
		return self::_get($a);
	}
    /**
     * check friend and friend request
     *
     * @param big_int $memberOne
     * @param big_int $memberTwo
     * @return object
     */
    public static function check($memberOne,$memberTwo){
        if($memberOne == $memberTwo) return true;
        list($memberOne, $memberTwo, $sender) = self::_order($memberOne, $memberTwo);
        $r = db::query(sprintf("SELECT sender,status FROM ilike
            WHERE memberOne = %d AND memberTwo = %d LIMIT 1;",
            $memberOne,$memberTwo),true);
        return $r->status=='match'?$r->status:($sender==$r->sender?'awaiting':$r->status);
    }

    public static function notification($ID, $sender, $item = null){
        $notification = array(
            'likeback' => sprintf(_('%s like back to you. %sclick here to mutual like%s.')
                    ,'<a href="http://'.DOMAIN.$sender->url.'">'.$sender->fullname.'</a>'
                    ,'<a href="http://'.DOMAIN.'/'.LANG.'/ilike">','</a>')
            );
        return $notification[$ID];
    }
}
?>